import api from '../../api/api'
import { urlObj } from '../../api/url'
import md5 from '../../utils/newmd5'
let APPID = 'wx279ac442a6dc50e4'
let SECRET = 'c5ec47daca6423fd1511020c20d9b2d2'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    password: '',
    showLoading: false,
    remember: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let username = wx.getStorageSync('username')
    let password = wx.getStorageSync('password')
    if(username) {
      this.setData({
        username: username,
        password: password,
        isRemember: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  password(e) {
    console.log(e)
    let { value } = e.detail;
    this.setData({
      password: value
    })
  },
  username(e) {
    let { value } = e.detail;
    this.setData({
      username: value
    })
  },
  handleLogin(e) {
    if (this.data.username && this.data.password) {
      wx.showLoading({
        title: '加载中',
      })
      const params = {
        loginname: this.data.username,
        password: md5(this.data.password)
      }
      console.log(params)
      api.post(urlObj.login, params).then(res => {
        wx.hideLoading()
        if (res.statusCode == '200') {
          let { token, info } = res.data;
          wx.setStorageSync('token', token)
          wx.setStorageSync('username', this.data.username)
          wx.setStorageSync('password', this.data.password)
          app.globalData.userInfo = info;
          if (info.isOpenId === 0) {
            this.wxgetUserInfo()
          } else {
            wx.switchTab({
              url: '../map/map',
            })
          }
        } else {
          console.log('1')
          wx.showToast({
            title: res.msg
          })
        }
      })
    } else {
      wx.showToast({
        title: '请输入账号密码',
        icon: 'none'
      })
    }

  },
  openPwdPage(e) {
    wx.navigateTo({
      url: '../password/password'
    })
  },
  openwxPage(){
    wx.switchTab({
      url: '../products/products',
    })
  },
  wxgetUserInfo: function () {
    // if (app.globalData.hasLogin == false) {
      wx.login({
        success: function (data) {
          console.log('获取登录 Code：' + data.code)
          const params = {
            code: data.code,
            encryptedData: app.globalData.wxusersresult.encryptedData,
            iv: app.globalData.wxusersresult.iv
          };
          api.post(urlObj.getOpenId, params).then(res => {
            if (res.statusCode === 200) {
              wx.switchTab({
                url: '../map/map',
              })
            } else {
              console.log(err)
            }
          }).catch(err => {
            console.log(err)
          })
        }
      })
    // }
    // else if (app.globalData.hasLogin==true) {
    //         wx.navigateTo({
    //           url: '../archives/archives'
    //         })
    // }
  },
  checkboxChange(e){
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    let val = e.detail.value
    if(val && val.length > 0) {
      this.setData({
        isRemember: true
      })
    }else{
      this.setData({
        isRemember: false
      })
    }
  }
})